# Maintainer: Naveen M K <naveen521kk@gmail.com>

_realname=bat
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.25.0
pkgrel=2
pkgdesc="Cat clone with syntax highlighting and git integration (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/sharkdp/bat'
msys2_references=(
  'archlinux: bat'
  'purl: pkg:cargo/bat'
  "cpe: cpe:/a:bat_project:bat"
)
license=('spdx:MIT OR Apache-2.0')
depends=(
  "${MINGW_PACKAGE_PREFIX}-oniguruma"
  "${MINGW_PACKAGE_PREFIX}-zlib"
  "${MINGW_PACKAGE_PREFIX}-libgit2"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-rust"
  "${MINGW_PACKAGE_PREFIX}-pkgconf"
  'git'
)
options=('!strip')
source=("git+${url}.git#tag=v${pkgver}")
sha256sums=('d3f4ee9ca8ea0f5927901dc3e41875a1cb68b44959d7b8be0a210010d4434bc5')

prepare() {
  cd "${_realname}"

  # bump git2 crate (also updates cc)
  git cherry-pick -n 01680e444ba4273b17d2d6d85a19f7a5e7046820
  cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
}

build() {
  cd "${_realname}"

  LIBGIT2_NO_VENDOR=1 \
  RUSTONIG_DYNAMIC_LIBONIG=1 \
    cargo build --release --frozen
}

package() {
  cd "${_realname}"

  install -Dm755 target/release/bat "${pkgdir}${MINGW_PREFIX}/bin/bat"

  # Package licenses
  install -Dm644 LICENSE-{APACHE,MIT} -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"

  cd target/release/build

  # Package the man page
  find . -name bat.1 -type f -exec install -Dm644 {} \
    "${pkgdir}${MINGW_PREFIX}"/share/man/man1/bat.1 \;

  # Package the shell completions
  find . -name bat.bash -type f -exec install -Dm644 {} \
    "${pkgdir}${MINGW_PREFIX}"/share/bash-completion/completions/bat \;

  find . -name bat.zsh -type f -exec install -Dm644 {} \
    "${pkgdir}${MINGW_PREFIX}"/share/zsh/site-functions/_bat \;

  find . -name bat.fish -type f -exec install -Dm644 {} \
    "${pkgdir}${MINGW_PREFIX}"/share/fish/vendor_completions.d/bat.fish \;
}
